package pub.tbc.toolkit.http2;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import pub.tbc.toolkit.Objs;

import java.util.Map;

/**
 * @author tbc on 2016/9/18 11:49:05.
 */
public class HttpRequesterImpl extends AbstractHttpRequester implements HttpRequester {

    @Override
    public ResponseWrapper get(String url) throws RuntimeException {
        return get(url, null);
    }

    @Override
    public ResponseWrapper get(String url, Map<String, Object> param) throws RuntimeException {
        return get(url, null, param);
    }

    @Override
    public ResponseWrapper get(String url, Map<String, String> headers, Map<String, Object> param) throws RuntimeException {
        HttpGet get = new HttpGet(urlInit(url, param));
        get.setHeaders(getHeader(headers));
        return send(get);
    }

    @Override
    public ResponseWrapper delete(String url) {
        return delete(url, null);
    }

    @Override
    public ResponseWrapper delete(String url, Map<String, Object> param) {
        return delete(url, null, param);
    }

    @Override
    public ResponseWrapper delete(String url, Map<String, String> headers, Map<String, Object> param) {
        HttpDelete delete = new HttpDelete(urlInit(url, param));
        delete.setHeaders(getHeader(headers));
        return send(delete);
    }

    @Override
    public ResponseWrapper post(String url, Map<String, Object> param) {
        // 默认content-type是json
        return post(url, param, super.CONTENT_TYPE_JSON);
    }

    @Override
    public ResponseWrapper post(String url, Map<String, String> headers, Map<String, Object> param) {
        return post(url, headers, param, Objs.isEmpty(headers) ? null : headers.get("Content-Type"));
    }

    @Override
    public ResponseWrapper post(String url, Map<String, Object> param, String contentType) {
        return post(url, null, param, contentType);
    }

    @Override
    public ResponseWrapper post(String url, Map<String, String> headers, Map<String, Object> param, String contentType) {
        HttpPost post = new HttpPost(requestUrlInit(url));
        post.setHeaders(getHeader(headers));
        post.setEntity(getBody(param, contentType));
        return send(post);
    }

    @Override
    public ResponseWrapper put(String url, Map<String, Object> param) {
        return put(url, param, CONTENT_TYPE_JSON);
    }

    @Override
    public ResponseWrapper put(String url, Map<String, String> headers, Map<String, Object> param) {
        return put(url, headers, param, Objs.isEmpty(headers) ? null : headers.get("Content-Type"));
    }

    @Override
    public ResponseWrapper put(String url, Map<String, Object> param, String contentType) {
        return put(url, null, param, contentType);

    }

    @Override
    public ResponseWrapper put(String url, Map<String, String> headers, Map<String, Object> param, String contentType) {
        HttpPut put = new HttpPut(requestUrlInit(url));
        put.setHeaders(getHeader(headers));
        put.setEntity(getBody(param, contentType));
        return send(put);
    }
}
